package br.com.gerenciador_recursos.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import br.com.gerenciador_recursos.GerencidorRecursosBusinessException;
import br.com.gerenciador_recursos.NoSuchTaskException;
import br.com.gerenciador_recursos.RequiredFieldException;
import br.com.gerenciador_recursos.TaskAlreadyExistNameException;
import br.com.gerenciador_recursos.model.Task;

public class TaskLocalServiceUtil {

	private static List<Task> tasks;

	static {
		tasks = new ArrayList<Task>();
	}

	public static Task add(int projectId, String name, String description,
			Timer estimativa, int userIdExecutor, Date inicioPrevisto,
			Date inicioRealizado) throws GerencidorRecursosBusinessException {

		validate(true, null, projectId, name, description, estimativa,
				userIdExecutor, inicioPrevisto, inicioRealizado);

		Task task = new Task(IDGeneratorUtil.increment(), projectId, name,
				description, estimativa, userIdExecutor, inicioPrevisto,
				inicioRealizado);

		tasks.add(task);

		return task;
	}

	public static Task update(int taskId, int projectId, String name,
			String description, Timer estimativa, int userIdExecutor,
			Date inicioPrevisto, Date inicioRealizado)
			throws GerencidorRecursosBusinessException {

		Task task = findByPrimaryKey(taskId);

		validate(false, task, projectId, name, description, estimativa,
				userIdExecutor, inicioPrevisto, inicioRealizado);

		tasks.remove(task);

		task = new Task(taskId, projectId, name, description, estimativa,
				userIdExecutor, inicioPrevisto, inicioRealizado);

		tasks.add(task);

		return task;
	}

	public static Task findByPrimaryKey(int taskId) throws NoSuchTaskException {

		Task project = fetchByPrimaryKey(taskId);

		if (project != null)
			return project;

		throw new NoSuchTaskException();
	}

	public static void delete(int taskId) throws NoSuchTaskException {

		Task task = findByPrimaryKey(taskId);

		tasks.remove(task);
	}

	public static Task fetchByPrimaryKey(int taskId) {

		for (Task task : tasks) {
			if (task.getTaskId() == taskId)
				return task;
		}

		return null;
	}

	public static Task findByNome(int projectId, String name, boolean equal) {

		for (Task task : tasks) {

			if (task.getProjectId() == projectId) {

				if (equal && task.getName().trim().equals(name.trim()))
					return task;
				else if (task.getName().indexOf(name.trim()) > -1)
					return task;

			}

		}

		return null;

	}

	public static Task findByNome(String name, boolean equal) {

		for (Task task : tasks) {
			if (equal && task.getName().trim().equals(name.trim()))
				return task;
			else if (task.getName().indexOf(name.trim()) > -1)
				return task;
		}

		return null;
	}

	private static void validate(boolean isNew, Task oldTask, int projectId,
			String name, String description, Timer estimativa,
			int userIdExecutor, Date inicioPrevisto, Date inicioRealizado)
			throws GerencidorRecursosBusinessException {

		if (name == null || name.trim().equals(""))
			throw new RequiredFieldException();

		Task project = findByNome(projectId, name, true);

		if (project != null && project.equals(oldTask)) {
			throw new TaskAlreadyExistNameException();
		}

		if (isNew) {

		} else {

		}

	}

	public static List<Task> findAll() {

		return new ArrayList<Task>(tasks);
	}

	public static List<Task> findByUserIdExecutor(int userIdExecutor,
			Comparator<Task> comparator) {

		List<Task> result = new ArrayList<Task>();

		for (Task task : tasks) {
			if (task.getUserIdExecutor() == userIdExecutor)
				result.add(task);
		}

		if (comparator != null)
			Collections.sort(result, comparator);

		return result;
	}

}
